## Replication files for "Do Online Ads Influence Vote Choice?"
## anselm.rink@gmail.com
## This code replicates Table 4

rm(list = ls())
library(foreign)
library(stargazer)
library(magrittr)
setwd("")
data <- read.csv("data.csv") 


## Define sums of voters for first and second vote
sum_voters_combined_erst <- sum(data$voters_erst_16[data$treatment_rational ==1 | data$treatment_emotional ==1], na.rm = T)
sum_voters_combined_zweit <- sum(data$voters_zweit_16[data$treatment_rational ==1 | data$treatment_emotional ==1], na.rm = T)


## extract regression coefficients
com1coef <- summary(lm(cdu16erst ~ treated, data=data))$coefficients[2,1]
com2coef <- summary(lm(cdu16zweit ~ treated, data=data))$coefficients[2,1]
com3coef <- summary(lm(cdu16erst ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data))$coefficients[2,1]
com4coef <- summary(lm(cdu16zweit ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data))$coefficients[2,1]


## Define sums 
sum1c <- sum_voters_combined_erst * com1coef
sum2c <- sum_voters_combined_erst * com2coef
sum3c <- sum_voters_combined_erst * com3coef
sum4c <- sum_voters_combined_erst * com4coef

## Cost per vote
cpv_model1 <- 40000/(sum1c) %>% round(digits = 2)
cpv_model2 <- 40000/(sum2c) %>% round(digits = 2)
cpv_model3 <- 40000/(sum3c) %>% round(digits = 2)
cpv_model4 <- 40000/(sum4c) %>% round(digits = 2)


## Define upper bound -- does not really make sense, hence infinity!
sum1c_upper <- sum_voters_combined_erst * confint(lm(cdu16erst ~ treated, data=data), 'treated', level=0.95)[1]
sum2c_upper <- sum_voters_combined_erst * confint(lm(cdu16zweit ~ treated, data=data), 'treated', level=0.95)[1]
sum3c_upper <- sum_voters_combined_erst * confint(lm(cdu16erst ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), 'treated', level=0.95)[1]
sum4c_upper <- sum_voters_combined_erst * confint(lm(cdu16zweit ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), 'treated', level=0.95)[1]

cpv_model1_upper <- 40000/(sum1c_upper) %>% round(digits = 2)
cpv_model2_upper <- 40000/(sum2c_upper) %>% round(digits = 2)
cpv_model3_upper <- 40000/(sum3c_upper) %>% round(digits = 2)
cpv_model4_upper <- 40000/(sum4c_upper) %>% round(digits = 2)


## Define lower bound -- does not really make sense, hence infinity!
sum1c_lower <- sum_voters_combined_erst * confint(lm(cdu16erst ~ treated, data=data), 'treated', level=0.95)[2]
sum2c_lower <- sum_voters_combined_erst * confint(lm(cdu16zweit ~ treated, data=data), 'treated', level=0.95)[2]
sum3c_lower <- sum_voters_combined_erst * confint(lm(cdu16erst ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), 'treated', level=0.95)[2]
sum4c_lower <- sum_voters_combined_erst * confint(lm(cdu16zweit ~ treated + age6_15 + age15_18 + age18_27 + age27_45 + age45_55 + age55_65 + age_o65 + female + protestant + catholic + atheist_pop + foreign_pop + total_pop_ln + cdu11erst, data=data), 'treated', level=0.95)[2]

cpv_model1_lower <- 40000/(sum1c_lower) %>% round(digits = 2)
cpv_model2_lower <- 40000/(sum2c_lower) %>% round(digits = 2)
cpv_model3_lower <- 40000/(sum3c_lower) %>% round(digits = 2)
cpv_model4_lower <- 40000/(sum4c_lower) %>% round(digits = 2)


## CI 95%
CI_1 <- paste0("[", round(cpv_model1_lower, digit = 2), ";", round(cpv_model1_upper, digit = 2),"]")
CI_2 <- paste0("[", round(cpv_model2_lower, digit = 2), ";", round(cpv_model2_upper, digit = 2),"]")
CI_3 <- paste0("[", round(cpv_model3_lower, digit = 2), ";", round(cpv_model3_upper, digit = 2),"]")
CI_4 <- paste0("[", round(cpv_model4_lower, digit = 2), ";", round(cpv_model4_upper, digit = 2),"]")


## Create matrix 
table <- t(data.frame(c(cpv_model1, cpv_model2, cpv_model3, cpv_model4), c(CI_1, CI_2, CI_3, CI_4)))
rownames(table) <- c("Cost-per-vote", "95% CI")
stargazer(table)
